package com.blog4j.user.config;

import com.blog4j.common.interceptor.ApiCheckInterceptor;
import com.blog4j.common.utils.ExcelUtil;
import com.blog4j.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 功能描述
 * @Create on : 2024/7/14 20:13
 **/
@Configuration
public class WebConfigure implements WebMvcConfigurer {
    @Value("${custom.taskExecutor.corePoolSize}")
    private int corePoolSize;

    @Value("${custom.taskExecutor.maxPoolSize}")
    private int maxPoolSize;

    @Value("${custom.taskExecutor.queueCapacity}")
    private int queueCapacity;

    @Value("${custom.taskExecutor.threadNamePrefix}")
    private String threadNamePrefix;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ApiCheckInterceptor())
                .addPathPatterns("/**");
    }

    @Bean
    public ExcelUtil excelUtil() {
        return new ExcelUtil();
    }

    @Bean
    public RedisUtil redisUtil() {
        return new RedisUtil();
    }

    @Bean("customExecutor")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix(threadNamePrefix);
        executor.initialize();
        return executor;
    }
}
